﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.ViewManagement;
#if NETFX_CORE
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

#else
using System.Windows;
using System.Windows.Controls;
#endif

namespace StyleMVVM.View
{
	public sealed class VariableSizedWrapGridHelper
	{
		public static int GetLandscapeNumColumns(DependencyObject obj)
		{
			return (int)obj.GetValue(InternalLandscapeNumColumnsProperty);
		}

		public static void SetLandscapeNumColumns(DependencyObject obj, int value)
		{
			obj.SetValue(InternalLandscapeNumColumnsProperty, value);
		}

		// Using a DependencyProperty as the backing store for LandscapeNumColumns.  This enables animation, styling, binding, etc...
		internal static readonly DependencyProperty InternalLandscapeNumColumnsProperty =
			DependencyProperty.RegisterAttached("LandscapeNumColumns",
			                                    typeof(int),
			                                    typeof(VariableSizedWrapGridHelper),
			                                    new PropertyMetadata(0, SetGridColumns));

		public static int GetPortraitNumColumns(DependencyObject obj)
		{
			return (int)obj.GetValue(InternalPortraitNumColumnsProperty);
		}

		public static void SetPortraitNumColumns(DependencyObject obj, int value)
		{
			obj.SetValue(InternalPortraitNumColumnsProperty, value);
		}

		public static DependencyProperty PortraitNumColumnsProperty
		{
			get { return InternalPortraitNumColumnsProperty; }
		}

		// Using a DependencyProperty as the backing store for PortraitNumColumns.  This enables animation, styling, binding, etc...
		internal static readonly DependencyProperty InternalPortraitNumColumnsProperty =
			DependencyProperty.RegisterAttached("PortraitNumColumns",
			                                    typeof(int),
			                                    typeof(VariableSizedWrapGridHelper),
			                                    new PropertyMetadata(0, SetGridColumns));

		private static void SetGridColumns(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			if (d is VariableSizedWrapGrid)
			{
				VariableSizedWrapGrid grid = d as VariableSizedWrapGrid;

				grid.SizeChanged -= GridOnSizeChanged;

				grid.SizeChanged += GridOnSizeChanged;

				SetGridColumns(grid);
			}
		}

		private static void GridOnSizeChanged(object sender, SizeChangedEventArgs sizeChangedEventArgs)
		{
			SetGridColumns((VariableSizedWrapGrid)sender);
		}

		private static void SetGridColumns(VariableSizedWrapGrid grid)
		{
			int newValue = 0;

			switch (ApplicationView.Value)
			{
				case ApplicationViewState.Snapped:
				case ApplicationViewState.FullScreenPortrait:
					newValue = GetPortraitNumColumns(grid);
					break;

				default:
					newValue = GetLandscapeNumColumns(grid);
					break;
			}

			if (newValue > 0)
			{
				grid.MaximumRowsOrColumns = newValue;
			}
		}
	}
}